<?php
if ( !defined( "IN_UCHOME" ) ) exit( "Access Denied" );

$op = $_GET['op'];
$ops = array('sure', 'love', 'save', 'invite');
if(empty($op) || !in_array($op, $ops)) showmessage('出错了！这个请求已经过期了！', 'house.php?ac=index', 2);

$js = "<script type='text/javascript'>setTimeout(function(){window.location.href='house.php?ac=index';}, 1000);</script>";

$oer = intval($_GET['uid']);
$id = intval($_GET['id']);
$hash = intval($_GET['hash']);
$fuid = $uid;

if($op == 'sure'){//请求对方租房
	if(false === ($check = check_exists($oer, $fuid, $id, $hash, $op))) showmessage('出错了！你提交了非法的请求！', 'house.php?ac=index', 2);
	$user = get_user_status($oer);
	realname_set($user['uid'], $user['username']);
	$house = get_house($id);
	if(empty($house)) showmessage('出错了！TA已经把房子给卖了，你可以去租TA的其他房子！', 'house.php?ac=house&uid=' . $oer, 5);
	if($house['status'] != 1 || $house['sent_uid'] > 0) showmessage('抱歉！TA已经把房子租出去了，你可以去租TA的其他房子！', 'house.php?ac=house&uid=' . $oer, 5);
	if(isset($_POST['myFormAc'])){
		header('Content-type: text/xml; charset=' . $_SC['charset']);
		if($status['live_id'] > 0 || $status['hid'] > 0) $data = array('status'=>404, 'message'=>'出错了！你当前已经住在房子里了，请先把现在的房子退掉！<a href="house.php?ac=index" target="_blank">我去退房</a>');
		else{
			if($_POST['myForm'] == 'ok'){//同意租房
				query_exec("update ".tname("app_love_user")." set live_id = '{$id}' where uid = '{$status['uid']}' ");
				query_exec("update ".tname("app_love_house")." set status = 4, sent_uid = '{$status['uid']}', sent_username = '{$status['username']}', sent_dateline = '{$_SERVER['REQUEST_TIME']}' where id = '{$id}' ");
				$data = array('status'=>200, 'message'=>'成功了！你已经住进这所房子里了！' );
			}else{
				$data = array('status'=>200, 'message'=>'你已经忽略了TA请你租房请求' );
			}
			delete_exists($oer, $fuid, $id, $hash, $op);
		}
		ajax_out(jsonencode($data));
	}
}elseif($op == 'love'){//请求对方合租
	if(false === ($check = check_exists($oer, $fuid, $id, $hash, $op))) showmessage('出错了！你提交了非法的请求！', 'house.php?ac=index', 2);
	$user = get_user_status($oer);
	realname_set($user['uid'], $user['username']);
	if($user['live_id'] < 1) showmessage('出错了！TA现在还在外面流浪呢！怎么和你同居啊？', 'house.php?ac=index&uid=' . $oer, 2);
	if($user['hid'] > 0 || $user['live_with_uid'] > 0 || $user['user_status'] > 0) showmessage('出错了！TA已经在和别人同居了！', 'house.php?ac=index&uid=' . $oer, 2);
	$house = get_house($user['live_id']);
	if(empty($house)) showmessage('出错了！TA的房子已经不存在了，不能和你合租！', 'house.php?ac=house&uid=' . $oer, 2);
	if(isset($_POST['myFormAc'])){
		header('Content-type: text/xml; charset=' . $_SC['charset']);
		if($_POST['myForm'] == 'ok'){
			if($status['live_id'] > 0 || $status['hid'] > 0) $data = array('status'=>404, 'message'=>'出错了！你当前已经住在房子里了，请先把现在的房子退掉！<a href="house.php?ac=index" target="_blank">我去退房</a>');
			
			if(check_user_sex($user['uid'], $_SGLOBAL['supe_uid']) === false) {
				$data = array('status'=>404, 'message'=>'<p><b>抱歉哦，同性恋似乎不大好吧！<b></p>');
				ajax_out(jsonencode($data));
			}
				
			$hid = new_love($user['uid'], $user['username']);
			query_exec("update ".tname("app_love_user")." set hid= '{$hid}', love_num = love_num + 1, user_status = 1, live_id = '{$user['live_id']}'   where uid = '{$_SGLOBAL['supe_uid']}' ");
			query_exec("update ".tname("app_love_user")." set hid= '{$hid}', love_num = love_num + 1, user_status = 1 where uid = '{$user['uid']}' ");
			
			realname_set($user['uid'], $user['username']);
			realname_get();

			/*add news*/
			$data = array($_SGLOBAL['supe_uid'], $_SN[$_SGLOBAL['supe_uid']], $user['uid'], $_SN[$user['uid']]);
			news::add($data , 0, 0);

			/*add feeds*/
			$data = array(
				'user_1'=>"<a href='house.php?ac=index&uid={$_SGLOBAL['supe_uid']}'>{$_SN[$_SGLOBAL['supe_uid']]}</a>",
				'user'=> "<a href='house.php?ac=index&uid={$user['uid']}'>{$_SN[$user['uid']]}</a>",
			);
			feeds::add($data, 0);

			$data = array(
				'user_1'=>"<a href='house.php?ac=index&uid={$_SGLOBAL['supe_uid']}'>{$_SN[$_SGLOBAL['supe_uid']]}</a>",
				'user'=> "<a href='house.php?ac=index&uid={$user['uid']}'>{$_SN[$user['uid']]}</a>",
			);
			feeds::add($data, 0, $user);


			$data = array('status'=>200, 'message'=>'恭喜你！你和TA已经开始同居了' . $js);
		}else{
			$data = array('status'=>200, 'message'=>'你已经忽略了TA的同居请求！' . $js);
		}
		delete_exists($oer, $fuid, $id, $hash, $op);
		ajax_out(jsonencode($data));
	}
}elseif($op == 'save'){
	$sid = intval($_GET['sid']);
	$hash = intval($_GET['hash']);
	$data = query_one("select  *  from ".tname("app_love_save")." where sid = '{$sid}' and hash = '{$hash}'");
	if(empty($data)) showmessage('出错了！你提交了非法的请求！', 'house.php?ac=index', 2);
	if($data['uid'] == $status['uid']) showmessage('出错了！自己不能就自己，如果有钱的话，你可以把TA踢出来！', 'house.php?ac=index', 2);
	if($data['status'] == 0) showmessage('出错了！TA已经被别人从恶魔的手中拯救出来了，多谢你的关心！', 'house.php?ac=index', 3);
	$m = intval($house_config['focus'] / 2);
	$user = get_user_status($data['uid']);
	if($_POST['myForm'] == 'ok'){
		if($status['money'] < $m) showmessage('出错了！你拥有的金币还不能"英雄救美"！', 'house.php?ac=index', 3);
		query_exec("update  ".tname("app_love_user")." set money = money - {$m} where uid = '{$status['uid']}'");
		query_exec("update  ".tname("app_love_save")." set status = 0 where sid = '{$data['sid']}'");
		query_exec("update ".tname("app_love_history")." set status = 0 where hid = '{$data['hid']}' ");
		query_exec("update ".tname("app_love_user")."  set user_status = 0, live_id = 0, hid = 0 where uid = '{$user['uid']}' ");
		query_exec("update ".tname("app_love_user")."  set user_status = 0, live_id = 0, hid = 0 where uid = '{$user['live_with_uid']}' ");
		query_exec("update ".tname("app_love_house")."  set status = 1, sent_uid = 0, sent_username = 0, sent_dateline = 0 where id = '{$user['live_id']}' ");
		realname_get();
		/*add news*/
		$data = array($user['uid'], $_SN[$user['uid']], $_SGLOBAL['supe_uid'], $_SN[$_SGLOBAL['supe_uid']]);
		news::add($data , 1, 3);

		include_once(S_ROOT.'./source/function_cp.php');
		$note = "<font color=green>拯救信息</font>：在游戏[<a href='house.php'>新同居时代</a>]中 <a href='house.php?ac=index&uid={$_SGLOBAL['supe_uid']}'>{$_SN[$_SGLOBAL['supe_uid']]}</a> 把你从恶魔的手中拯救出来了";
		notification_add($user['uid'], 'love', $note);

		/*add feeds*/
		$data = array('user'=> "<a href='house.php?ac=index&uid={$user['uid']}'>{$_SN[$user['uid']]}</a>",);
		feeds::add($data, 3);

		showmessage('恭喜你！"英雄救美"成功，系统将转向TA的页面！', 'house.php?ac=index&uid=' . $data['uid'], 3);
	}
}elseif($op == 'invite'){
	$s_uid = intval($_GET['uid']);
	$check = query_one("select * from ".tname("app_love_invite")." where uid = '{$s_uid}' and fuid = '{$status['uid']}' ");
	if(empty($check)) showmessage('出错了！你提交了非法的请求！', 'house.php?ac=index', 2);
	if($check['status'] == 0) showmessage('出错了！这个请求已经过期了！', 'house.php?ac=index', 2);
	query_exec("update  ".tname("app_love_invite")." set status = 0 where uid = '{$s_uid}' and fuid = '{$status['uid']}'");
	query_exec("update  ".tname("app_love_user")." set money = money + 2000  where uid = '{$s_uid}' ");

	/*add news*/
	$data = array($s_uid, get_real_name($s_uid), 2000);
	news::add($data , 6, 8);

	showmessage('恭喜你进入新同居时代！', 'house.php?ac=index', 2);
}

realname_get();
include_once( template( "house/view/receive" ) );

function check_exists($uid, $fuid, $id, $hash, $op)
{
	$temp = query_one("select * from ".tname("app_love_renting")." where uid = '{$uid}' and fuid = '{$fuid}' and id = '{$id}' and hash = '{$hash}' and op = '{$op}'");
	if(empty($temp)) return false;
	else return $temp;
}

function delete_exists($uid, $fuid, $id, $hash, $op)
{
	query_exec("delete from ".tname("app_love_renting")." where  uid = '{$uid}' and fuid = '{$fuid}' and id = '{$id}' and hash = '{$hash}' and op = '{$op}' ");
}
?>